var ox=0;
window.onload=function(){
	$("#new-handle").on('click',function(){
		var x=$(".desk-num").html();
		$("#desk-"+x).attr("class","desk-yes");
		$("#new-menu").hide();
		$("#menu").show();
	})
	$(".desk div").click(function(){
		var i=$(this).attr("class").split("-")[1];
		var x=$(this).attr("id").split("-")[1];
		$(".desk-num").html(x)
		if (i=='new') {
			$("#menu").hide();
			$("#new-menu").show();
			$(".desk-people").html("5");
			$(".desk-state").html("有人");
		}else if(i=='yes'){
			$("#new-menu").hide();
			$("#menu").show();
			$(".desk-people").html("5");
			$(".desk-state").html("有人");
		}else if(i=='no'){
			$("#new-menu").hide();
			$("#menu").hide();
			$(".desk-people").html("5");
			$(".desk-state").html("无人");
		}
	})
	$("#head button").click(function(){
		var r=confirm("你确定要退出登录！");
		if (r==true){
			logout();
		}
	});
	$(".radioo").click(function(){
		$("#people").slideUp();
		ox=0;
	})
	$(".radiox").click(function(){
		$("#people").slideDown();
		ox=1;
	})
	findHelpPosition();
	var positionid = $("#positions").val();
	if(positionid!=null){
		seeOrder(positionid)
	}
	$("#positions").change(function(){
		var positionid = $("#positions").val();
		if(positionid!="underfine"){
			seeOrder(positionid)
		}
	});
	$("#foods").on('click', '.reduce',function(){
		var positionid = $("#positions").val();
		var orderid = $(this).attr("name");
		var num = prompt("输入退回数量:","1");
		if(parseInt(num)==num&&num!=null){
			reduceFood(positionid,orderid,num);
		}
	});
	$("#btn-alt").click(function(){//验证推荐人
		$.ajax({
			type:"get",
			url:"/ldss/customers/validate",
			async:false,
			data:{
				tel:$("*[name='three-oldphone']").val(),
			},
			dataType:"json",
			success: function (data) {
				if(data.success){
					sessionStorage.refereeId = data.message;
					$(".new_name").removeAttr("disabled");
					$(".new_tel").removeAttr("disabled");
					$(".new_pwd").removeAttr("disabled");
					$(".new_rpwd").removeAttr("disabled");
					$("#alt-red").hide();
				}
				else{
					$("#alt-red").show();
					$(".new_name").attr("disabled", true);
					$(".new_tel").attr("disabled", true);
					$(".new_pwd").attr("disabled", true);
					$(".new_rpwd").attr("disabled", true);
					alert("此人还未出生！")
				}
			},
			error: function(){
				alert("请求出错");
			}
		})
	});
	$("#flow").click(function(){//留帐
		alert("暂时不能留帐")
	});
	$("#three-submit").click(function(){//结账
		if(ox==0){
			var refereetel = -1;
		}else{
			refereetel = $("*[name='three-oldphone']").val();
			if(refereetel==""||refereetel==null){
				alert("请输入推荐人手机号码");
				return;
			}
			var new_name = $("*[name='three-name']").val();
			if(new_name==""||new_name==null){
				alert("请输入顾客名称");
				return;
			}
			var new_tel = $("*[name='three-phone']").val();
			if(new_tel==""||new_tel==null){
				alert("请输入顾客手机号码");
				return;
			}
			var new_pwd = $("*[name='three-password']").val();
			if(new_pwd==""||new_pwd==null){
				alert("请输入密码");
				return;
			}
			var new_rpwd = $("*[name='three-bpassword']").val();
			if(new_rpwd!=new_pwd){
				alert("两次输入密码不同");
				return;
			}
		}
		closeT(refereetel,new_name,new_tel,new_pwd);
	});
	//刷新按钮
	$("#refresh").click(function(){
		findHelpPosition();
	})
};

function closeT(refereetel,new_name,new_tel,new_pwd){//结账
	$.ajax({
		type:"get",
		url:"/ldss/transaction/closet",
		async:false,
		data:{
				positionid:$("#positions").val(),
				refereetel:refereetel,
				newname:new_name,
				newtel:new_tel,
				newpsswd:new_pwd
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data;
				alert(data.message);
				findHelpPosition();
				var positionid = $("#positions").val();
				$("#foods").empty();
				$("#people").slideUp();
				ox=0;
				if(positionid!=null){
					seeOrder(positionid)
				}
			}else{
				alert(data.message);
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}


function findHelpPosition(){//查找需要帮助的位置
	$.ajax({
		type:"get",
		url:"/ldss/orders/showcloseposition",
		dataType:"json",
		async:false,
		success: function (data) {
			if(data.success){
				var data = data.data;
				$("#positions").empty();
				for(var i = 0; i < data.length; i++){
					$("#positions").append("<option value='"+data[i].id+"'>"+data[i].positionName+"</option>");
				}
			}else{
				alert(data.message);
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}
function seeOrder(positionid){
	$.ajax({
		type:"get",
		url:"/ldss/orders/showmenu",
		async:false,
		data:{
				positionid:positionid
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data.data;
				$("#foods").empty();
				var allprise = 0;
				for(var i = 0; i < data.length; i++){
					var everpeist = data[i].num*data[i].menuprise;
					allprise = allprise+everpeist;
					$("#foods").append(
						"<tr>"+
							"<td>"+data[i].menuname+"</td>"+
							"<td>"+data[i].menuprise+"</td>"+
							"<td>"+data[i].num+"</td>"+
							"<td>"+everpeist+"</td>"+
							"<td>"+data[i].ordertype+"</td>"+
							"<td>"+ 
								"<button type='button' class='btn btn-primary reduce' name='"+data[i].id+"'退回</button>"+
							"</td>"+
						"</tr>"
					);
				}
				$("#foods").append(
					"<tr>"+
						"<td colspan='3'>总价</td>"+
						"<td colspan='3'>"+allprise+"</td>"+
					"</tr>"
				);
			}
		},
		error:function(){
			alert("请求数据失败");
		}
	});
}
function seeMenus(positionid,positionname){//查看这个位置有什么需求
	$.ajax({
		type:"get",
		url:"/ldss/orders/showhelpmenu",
		async:false,
		data:{
			positionid:positionid
		},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data.data;
				$("#foods").empty();
				if(data.length>0){
					for(var i = 0; i < data.length; i++){
						$("#foods").append(
							"<tr>"+
							"<td>"+positionname+"</td>"+
							"<td>"+data[i].menuname+"</td>"+
							"<td>"+data[i].num+"</td>"+
							"<td>"+data[i].ordertype+"</td>"+
							"<td>"+ 
							"<button type='button' class='btn btn-primary recfood' name='"+data[i].id+"'>处理</button>"+
							"</td>"+
							"</tr>"
							);
					}
					$(this).removeClass("list-group-item-danger");
				}else{
					alert("该位置暂时没有需求")
				}
				
			}else{
				alert(data.message);
			}
		}
	});
}
function reduceFood(positionid,orderid,num){
	$.ajax({
		type:"get",
		url:"/ldss/orders/reduce",
		async:false,
		data:{
				orderid:orderid,
				num:num
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data;
				alert(data.message);
				seeOrder(positionid);
			}else{
				alert(data.message);
			}
		}
	});
}
function logout(){
	$.ajax({
		type:"get",
		url:"/ldss/user/logout",
		dataType:"json",
		async:false,
		success: function (data) {
			if(data.success){
				window.location.href=("/ldss/ldss/login");
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}